package com.s4j.struts.biz.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.Action;
import com.s4j.core.common.BaseAction;
import com.s4j.core.common.MyDateUtils;
import com.s4j.core.common.StringUtils;
import com.s4j.core.conset.Constant;
import com.s4j.core.datagrid.DatagridPaging;
import com.s4j.core.manager.model.SysModule;
import com.s4j.core.manager.model.SysUser;
import com.s4j.struts.biz.model.BizMgAppr;
import com.s4j.struts.biz.model.BizMgClinical;
import com.s4j.struts.biz.model.BizMgMark;
import com.s4j.struts.biz.model.BizMgSpecific;
import com.s4j.struts.biz.model.BizMgSupplement;
import com.s4j.struts.biz.model.BizMgSyndresult;
import com.s4j.struts.biz.model.BizMgTreathis;
import com.s4j.struts.biz.model.CombinedDisease;
import com.s4j.struts.biz.model.MgResultDtl;
import com.s4j.struts.biz.model.MgSuppImmun;
import com.s4j.struts.biz.model.MgSuppInspect;
import com.s4j.struts.biz.model.MgTreathisCm;
import com.s4j.struts.biz.model.MgTreathisWm;
import com.s4j.struts.biz.model.PatientDoc;
import com.s4j.struts.biz.service.MyastheniaDocService;

@Controller("com.s4j.struts.biz.action.MyastheniaDocAction")
@Scope("prototype")
public class MyastheniaDocAction extends BaseAction {
	
	@Resource
	private MyastheniaDocService mDocService;
	
	private PatientDoc patientDoc;
	
	private BizMgAppr mgAppr;
	
	private BizMgSpecific spec;
	
	private BizMgTreathis treathis;
	
	private BizMgMark mark;
	
	private BizMgClinical clinical;
	
	private BizMgSyndresult syndresult;
	
	private BizMgSupplement supplement;

	public String mainQuery() {
		
		DatagridPaging datagridPaging = mDocService.findUserByPaging(getPage(), getRows(), patientDoc);
		
		this.setDatagridPaging(datagridPaging);
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_showTab() {
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_commonSave() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		SysUser login = (SysUser)session.getAttribute(Constant.BG_LOGIN_USER);
		if(patientDoc.getDocId() == null) {
			patientDoc.setDiseaseType(Constant.PATIENTDOC_DISEASETYPE_MG);
			patientDoc.setInputDate(MyDateUtils.getCurDateTime());
			patientDoc.setInputUserName(login.getUserName());
			mDocService.saveCommon(patientDoc);
		} else {
			patientDoc.setDiseaseType(Constant.PATIENTDOC_DISEASETYPE_MG);
			mDocService.updateCommon(patientDoc);
		}
		
		this.getResultMap().put("docId", patientDoc.getDocId());
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_apprSave1() throws Exception {
		if(mgAppr.getApprId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			mgAppr.setPatient(patient);
			if(!StringUtils.isNullOrBlank(mgAppr.getQuezhen_day())) {
				int age = MyDateUtils.differenceDate("yyyy-MM-dd", patient.getBirthday(), mgAppr.getQuezhen_day());
				mgAppr.setQuezhen_age(age/12);
			}
			mDocService.save(mgAppr);
		} else {
			BizMgAppr persis = mDocService.get(BizMgAppr.class, mgAppr.getApprId());
			persis.setHbflag(mgAppr.getHbflag());
			persis.setQibing_day(mgAppr.getQibing_day());
			persis.setQuezhen_day(mgAppr.getQuezhen_day());
			persis.setQuezhen_hospital(mgAppr.getQuezhen_hospital());
			persis.setZhenduan_standard(mgAppr.getZhenduan_standard());
			if(!StringUtils.isNullOrBlank(mgAppr.getQuezhen_day())) {
				int age = MyDateUtils.differenceMonth("yyyy-MM-dd", persis.getPatient().getBirthday(), mgAppr.getQuezhen_day());
				persis.setQuezhen_age(age/12);
			} else {
				persis.setQuezhen_age(null);
			}
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_apprSave2() {
		if(mgAppr.getApprId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			mgAppr.setPatient(patient);
			mDocService.save(mgAppr);
		} else {
			BizMgAppr persis = mDocService.get(BizMgAppr.class, mgAppr.getApprId());
			persis.setClinic_type(mgAppr.getClinic_type());
			persis.setMuscle_eye(mgAppr.getMuscle_eye());
			persis.setMuscle_face(mgAppr.getMuscle_face());
			persis.setMuscle_spinal(mgAppr.getMuscle_spinal());
			persis.setMuscle_neck(mgAppr.getMuscle_neck());
			persis.setMuscle_breath(mgAppr.getMuscle_breath());
			persis.setMuscle_limb(mgAppr.getMuscle_limb());
			persis.setMuscle_other(mgAppr.getMuscle_other());
			persis.setComp_thymicHype(mgAppr.getComp_thymicHype());
			persis.setComp_thymoma(mgAppr.getComp_thymoma());
			persis.setComp_thymusDegen(mgAppr.getComp_thymusDegen());
			persis.setComp_thyroid(mgAppr.getComp_thyroid());
			persis.setComp_thyroidFunHyper(mgAppr.getComp_thyroidFunHyper());
			persis.setComp_other(mgAppr.getComp_other());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_apprSave3() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] diseases = request.getParameterValues("diseases");
		if(mgAppr.getApprId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			mgAppr.setPatient(patient);
			List<CombinedDisease> diseaseList = new ArrayList<CombinedDisease>();
			for(int i=0; i<diseases.length; i++) {
				String disease = diseases[i];
				if(!StringUtils.isNullOrBlank(disease)) {
					CombinedDisease cd = new CombinedDisease();
					cd.setCdname(disease);
					cd.setMgAppr(mgAppr);
					diseaseList.add(cd);
				}
			}
			mgAppr.setDiseases(diseaseList);
			mDocService.save(mgAppr);
		} else {
			BizMgAppr persis = mDocService.get(BizMgAppr.class, mgAppr.getApprId());
			
			mDocService.deleteMgApprDiseases(persis);
			
			List<CombinedDisease> diseaseList = new ArrayList<CombinedDisease>();
			for(int i=0; i<diseases.length; i++) {
				String disease = diseases[i];
				if(!StringUtils.isNullOrBlank(disease)) {
					CombinedDisease cd = new CombinedDisease();
					cd.setCdname(disease);
					cd.setMgAppr(persis);
					diseaseList.add(cd);
				}
			}
			persis.setDiseases(diseaseList);
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_apprSave4() {
		if(mgAppr.getApprId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			mgAppr.setPatient(patient);
			mDocService.save(mgAppr);
		} else {
			BizMgAppr persis = mDocService.get(BizMgAppr.class, mgAppr.getApprId());
			persis.setInduce(mgAppr.getInduce());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_specificSave() {
		if(spec.getSpecId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			spec.setPatient(patient);
			mDocService.save(spec);
		} else {
			BizMgSpecific persis = mDocService.get(BizMgSpecific.class, spec.getSpecId());
			persis.setMode(spec.getMode());
			persis.setModeTxt(spec.getModeTxt());
			persis.setCourse(spec.getCourse());
			persis.setCourseTxt(spec.getCourseTxt());
			persis.setTrait(spec.getTrait());
			persis.setTraitTxt(spec.getTraitTxt());
			persis.setPhysique(spec.getPhysique());
			persis.setPhysiqueTxt(spec.getPhysiqueTxt());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_treathisSave1() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] wmId = request.getParameterValues("wmId");
		String[] name = request.getParameterValues("wmName");
		String[] dosage = request.getParameterValues("wmDosage");
		String[] date = request.getParameterValues("wmDate");
		String[] maxDosage = request.getParameterValues("wmMaxDosage");
		String[] result = request.getParameterValues("wmResult");
		
		if(treathis.getTreathisId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			treathis.setPatient(patient);
			mDocService.save(treathis);
		}
		
		for(int i=0; i<name.length; i++) {
			if(StringUtils.isBlank(name[i])) {
				continue;
			}
			if(StringUtils.isBlank(wmId[i])) {
				MgTreathisWm wm = new MgTreathisWm();
				wm.setName(name[i].trim());
				wm.setDosage(dosage[i]);
				wm.setDate(date[i]);
				wm.setMaxDosage(maxDosage[i]);
				wm.setResult(result[i]);
				wm.setTreathis(treathis);
				mDocService.save(wm);
			} else {
				MgTreathisWm wm = mDocService.get(MgTreathisWm.class, new Long(wmId[i]));
				wm.setName(name[i]);
				wm.setDosage(dosage[i]);
				wm.setDate(date[i]);
				wm.setMaxDosage(maxDosage[i]);
				wm.setResult(result[i]);
				wm.setTreathis(treathis);
				mDocService.update(wm);
			}
		}
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_treathisSave2() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] cm_sDate = request.getParameterValues("cm_sDate");
		String[] cm_eDate = request.getParameterValues("cm_eDate");
		String[] cmName = request.getParameterValues("cmName");
		
		if(treathis.getTreathisId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			treathis.setPatient(patient);
			
			List<MgTreathisCm> cmList = new ArrayList<MgTreathisCm>();
			for(int i=0; i<cmName.length; i++) {
				if(!StringUtils.isNullOrBlank(cmName[i])) {
					MgTreathisCm cm = new MgTreathisCm();
					cm.setName(cmName[i]);
					cm.setsDate(cm_sDate[i]);
					cm.seteDate(cm_eDate[i]);
					cm.setTreathis(treathis);
					cmList.add(cm);
				}
			}
			
			treathis.setCm(cmList);
			
			mDocService.save(treathis);
		} else {
			BizMgTreathis persis = mDocService.get(BizMgTreathis.class, treathis.getTreathisId());
			
			mDocService.deleteMgTreaHistCm(persis);
			
			List<MgTreathisCm> cmList = new ArrayList<MgTreathisCm>();
			for(int i=0; i<cmName.length; i++) {
				if(!StringUtils.isNullOrBlank(cmName[i])) {
					MgTreathisCm cm = new MgTreathisCm();
					cm.setName(cmName[i]);
					cm.setsDate(cm_sDate[i]);
					cm.seteDate(cm_eDate[i]);
					cm.setTreathis(persis);
					cmList.add(cm);
				}
			}
			
			persis.setCm(cmList);
			
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_treathisSave3() {
		if(treathis.getTreathisId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			treathis.setPatient(patient);
			mDocService.save(treathis);
		} else {
			BizMgTreathis persis = mDocService.get(BizMgTreathis.class, treathis.getTreathisId());
			persis.setRespirator(treathis.getRespirator());
			persis.setRespirator_date(treathis.getRespirator_date());
			persis.setRespirator_type(treathis.getRespirator_type());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_treathisSave4() {
		if(treathis.getTreathisId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			treathis.setPatient(patient);
			mDocService.save(treathis);
		} else {
			BizMgTreathis persis = mDocService.get(BizMgTreathis.class, treathis.getTreathisId());
			persis.setThymusectomy(treathis.getThymusectomy());
			persis.setThymusectomy_date(treathis.getThymusectomy_date());
			persis.setThymusectomy_type(treathis.getThymusectomy_type());
			persis.setThymusectomy_rt(treathis.getThymusectomy_rt());
			persis.setThymusectomy_ct(treathis.getThymusectomy_ct());
			persis.setThymusectomy_pt(treathis.getThymusectomy_pt());
			persis.setThymusectomy_ex(treathis.getThymusectomy_ex());
			persis.setThymusectomy_rs(treathis.getThymusectomy_rs());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_markSave() {
		if(mark.getMarkId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			mark.setPatient(patient);
			mDocService.save(mark);
		} else {
			BizMgMark persis = mDocService.get(BizMgMark.class, mark.getMarkId());
			persis.setEyeDL(mark.getEyeDL());
			persis.setEyeDR(mark.getEyeDR());
			persis.setEyeUL(mark.getEyeUL());
			persis.setEyeUR(mark.getEyeUL());
			persis.setEyeBL(mark.getEyeBL());
			persis.setEyeBR(mark.getEyeBR());
			persis.setLimbUL(mark.getLimbUL());
			persis.setLimbUR(mark.getLimbUR());
			persis.setLimbDL(mark.getLimbDL());
			persis.setLimbDR(mark.getLimbDR());
			persis.setFace(mark.getFace());
			persis.setMouth(mark.getMouth());
			persis.setBreat(mark.getBreat());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_clinicalSave() {
		if(clinical.getClinicalId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			clinical.setPatient(patient);
			mDocService.save(clinical);
		} else {
			BizMgClinical persis = mDocService.get(BizMgClinical.class, clinical.getClinicalId());
			persis.setSpleen1(clinical.getSpleen1());
			persis.setSpleen2(clinical.getSpleen2());
			persis.setSpleen3(clinical.getSpleen3());
			persis.setSpleen4(clinical.getSpleen4());
			persis.setSpleen5(clinical.getSpleen5());
			persis.setSpleen6(clinical.getSpleen6());
			persis.setSpleen7(clinical.getSpleen7());
			persis.setSpleen8(clinical.getSpleen8());
			persis.setSpleen9(clinical.getSpleen9());
			persis.setSpleen10(clinical.getSpleen10());
			persis.setSpleen11(clinical.getSpleen11());
			persis.setSpleen12(clinical.getSpleen12());
			persis.setSpleen13(clinical.getSpleen13());
			persis.setSpleen14(clinical.getSpleen14());
			persis.setSpleen15(clinical.getSpleen15());
			persis.setSpleen16(clinical.getSpleen16());
			persis.setKidney1(clinical.getKidney1());
			persis.setKidney2(clinical.getKidney2());
			persis.setKidney3(clinical.getKidney3());
			persis.setKidney4(clinical.getKidney4());
			persis.setKidney5(clinical.getKidney5());
			persis.setKidney6(clinical.getKidney6());
			persis.setLiver1(clinical.getLiver1());
			persis.setLiver2(clinical.getLiver2());
			persis.setLiver3(clinical.getLiver3());
			persis.setLiver4(clinical.getLiver4());
			persis.setLiver5(clinical.getLiver5());
			persis.setLiver6(clinical.getLiver6());
			persis.setLiver7(clinical.getLiver7());
			persis.setLiver8(clinical.getLiver8());
			persis.setHeart1(clinical.getHeart1());
			persis.setHeart2(clinical.getHeart2());
			persis.setHeart3(clinical.getHeart3());
			persis.setLung1(clinical.getLung1());
			persis.setLung2(clinical.getLung2());
			persis.setLung3(clinical.getLung3());
			persis.setMouth1(clinical.getMouth1());
			persis.setMouth2(clinical.getMouth2());
			persis.setMouth3(clinical.getMouth3());
			persis.setMouth4(clinical.getMouth4());
			persis.setMouth5(clinical.getMouth5());
			persis.setMouth6(clinical.getMouth6());
			persis.setMouth7(clinical.getMouth7());
			persis.setMouth8(clinical.getMouth8());
			persis.setMouth9(clinical.getMouth9());
			persis.setMouth10(clinical.getMouth10());
			persis.setBodyA1(clinical.getBodyA1());
			persis.setBodyA2(clinical.getBodyA2());
			persis.setBodyB1(clinical.getBodyB1());
			persis.setBodyB2(clinical.getBodyB2());
			persis.setBodyB3(clinical.getBodyB3());
			persis.setBodyB4(clinical.getBodyB4());
			persis.setBodyB5(clinical.getBodyB5());
			persis.setBodyB6(clinical.getBodyB6());
			persis.setBodyB7(clinical.getBodyB7());
			persis.setBodyC1(clinical.getBodyC1());
			persis.setBodyC2(clinical.getBodyC2());
			persis.setBodyC3(clinical.getBodyC3());
			persis.setBodyC4(clinical.getBodyC4());
			persis.setBodyC5(clinical.getBodyC5());
			persis.setBodyC6(clinical.getBodyC6());
			persis.setBodyC7(clinical.getBodyC7());
			persis.setBodyC8(clinical.getBodyC8());
			persis.setBodyD1(clinical.getBodyD1());
			persis.setBodyD2(clinical.getBodyD2());
			persis.setBodyD3(clinical.getBodyD3());
			persis.setBodyD4(clinical.getBodyD4());
			persis.setBodyD5(clinical.getBodyD5());
			persis.setTongue(clinical.getTongue());
			persis.setPulse(clinical.getPulse());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_syndresultSave1() {
		if(syndresult.getSyndresId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			syndresult.setPatient(patient);
			mDocService.save(syndresult);
		} else {
			BizMgSyndresult persis = mDocService.get(BizMgSyndresult.class, syndresult.getSyndresId());
			persis.setSymptom(syndresult.getSymptom());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_syndresultSave2() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] resultDtl = request.getParameterValues("resultDtl");
		String[] resultDtlId = request.getParameterValues("resultDtlId");
		
		if(syndresult.getSyndresId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			syndresult.setPatient(patient);
			
			List<MgResultDtl> dtlList = new ArrayList<MgResultDtl>();
			for(int i=0; i<resultDtl.length; i++) {
				MgResultDtl dtl = new MgResultDtl();
				dtl.setName(resultDtl[i]);
				dtl.setSyndresult(syndresult);
				dtlList.add(dtl);
			}
			
			syndresult.setResultDtl(dtlList);
			
			mDocService.save(syndresult);
		} else {
			BizMgSyndresult persis = mDocService.get(BizMgSyndresult.class, syndresult.getSyndresId());
			
			for(int i=0; i<resultDtl.length; i++) {
				if(StringUtils.isBlank(resultDtl[i])) {
					if(!StringUtils.isBlank(resultDtlId[i])) {
						MgResultDtl dtl = mDocService.get(MgResultDtl.class, new Long(resultDtlId[i]));
						mDocService.delete(dtl);
					}
				} else {
					if(StringUtils.isBlank(resultDtlId[i])) {
						MgResultDtl dtl = new MgResultDtl();
						dtl.setName(resultDtl[i]);
						dtl.setSyndresult(persis);
						mDocService.save(dtl);
					} else {
						MgResultDtl dtl = mDocService.get(MgResultDtl.class, new Long(resultDtlId[i]));
						dtl.setName(resultDtl[i]);
						mDocService.update(dtl);
					}
				}
			}
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_syndresultSave3() {
		if(syndresult.getSyndresId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			syndresult.setPatient(patient);
			mDocService.save(syndresult);
		} else {
			BizMgSyndresult persis = mDocService.get(BizMgSyndresult.class, syndresult.getSyndresId());
			persis.setNature1(syndresult.getNature1());
			persis.setNature2(syndresult.getNature2());
			persis.setNature3(syndresult.getNature3());
			persis.setNature4(syndresult.getNature4());
			persis.setNature5(syndresult.getNature5());
			persis.setNature6(syndresult.getNature6());
			persis.setNature7(syndresult.getNature7());
			persis.setNature8(syndresult.getNature8());
			persis.setNature9(syndresult.getNature9());
			persis.setNatureOther(syndresult.getNatureOther());
			persis.setOrien1(syndresult.getOrien1());
			persis.setOrien2(syndresult.getOrien2());
			persis.setOrien3(syndresult.getOrien3());
			persis.setOrien4(syndresult.getOrien4());
			persis.setOrien5(syndresult.getOrien5());
			persis.setOrienOther(syndresult.getOrienOther());
			persis.setSelfResult(syndresult.getSelfResult());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_syndresultSave4() {
		if(syndresult.getSyndresId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			syndresult.setPatient(patient);
			mDocService.save(syndresult);
		} else {
			BizMgSyndresult persis = mDocService.get(BizMgSyndresult.class, syndresult.getSyndresId());
			persis.setConcl(syndresult.getConcl());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_supplementSave1() {
		if(supplement.getSuppId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			supplement.setPatient(patient);
			mDocService.save(supplement);
		} else {
			BizMgSupplement persis = mDocService.get(BizMgSupplement.class, supplement.getSuppId());
			persis.setAntibody(supplement.getAntibody());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_supplementSave2() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] igg = request.getParameterValues("igg");
		String[] c3 = request.getParameterValues("c3");
		String[] cd4 = request.getParameterValues("cd4");
		String[] cd8 = request.getParameterValues("cd8");
		String[] sil2r = request.getParameterValues("sil2r");
		String[] ifn = request.getParameterValues("ifn");
		String[] antissa = request.getParameterValues("antissa");
		String[] antissb = request.getParameterValues("antissb");
		String[] ana = request.getParameterValues("ana");
		
		if(supplement.getSuppId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			supplement.setPatient(patient);
			mDocService.save(supplement);
		} 
		
		BizMgSupplement persis = supplement;
		
		for(int i=0; i<4; i++) {
			int week = i == 0 ? 2 : (i == 1 ? 4 : (i == 2 ? 8 : 12));
			MgSuppImmun immun = mDocService.findImmuBySuppWeek(persis.getSuppId(), week);
			if(immun == null) {
				MgSuppImmun temp = new MgSuppImmun();
				temp.setSupp(persis);
				temp.setWeek(week);
				temp.setIgg(igg[i]);
				temp.setC3(c3[i]);
				temp.setCd4(cd4[i]);
				temp.setCd8(cd8[i]);
				temp.setSil2r(sil2r[i]);
				temp.setIfn(ifn[i]);
				temp.setAntissa(antissa[i]);
				temp.setAntissb(antissb[i]);
				temp.setAna(ana[i]);
				mDocService.save(temp);
			} else {
				immun.setIgg(igg[i]);
				immun.setC3(c3[i]);
				immun.setCd4(cd4[i]);
				immun.setCd8(cd8[i]);
				immun.setSil2r(sil2r[i]);
				immun.setIfn(ifn[i]);
				immun.setAntissa(antissa[i]);
				immun.setAntissb(antissb[i]);
				immun.setAna(ana[i]);
				mDocService.update(immun);
			}
		}
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_supplementSave3() {
		if(supplement.getSuppId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			supplement.setPatient(patient);
			mDocService.save(supplement);
		} else {
			BizMgSupplement persis = mDocService.get(BizMgSupplement.class, supplement.getSuppId());
			persis.setBiopsy(supplement.getBiopsy());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_supplementSave4() {
		if(supplement.getSuppId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			supplement.setPatient(patient);
			mDocService.save(supplement);
		} else {
			BizMgSupplement persis = mDocService.get(BizMgSupplement.class, supplement.getSuppId());
			persis.setEmg(supplement.getEmg());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_supplementSave5() {
		if(supplement.getSuppId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			supplement.setPatient(patient);
			mDocService.save(supplement);
		} else {
			BizMgSupplement persis = mDocService.get(BizMgSupplement.class, supplement.getSuppId());
			persis.setBloodGas(supplement.getBloodGas());
			mDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String myastheniaDoc_supplementSave6() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] bun = request.getParameterValues("bun");
		String[] alt = request.getParameterValues("alt");
		String[] blood = request.getParameterValues("blood");
		String[] urine = request.getParameterValues("urine");
		String[] sp = request.getParameterValues("sp");
		String[] crp = request.getParameterValues("crp");
		String[] esr = request.getParameterValues("esr");
		String[] ec = request.getParameterValues("ec");
		
		if(supplement.getSuppId() == null) {
			PatientDoc patient = mDocService.getPatientDoc(patientDoc.getDocId());
			supplement.setPatient(patient);
			mDocService.save(supplement);
		} 
		
		BizMgSupplement persis = supplement;
		
		for(int i=0; i<4; i++) {
			int week = i == 0 ? 2 : (i == 1 ? 4 : (i == 2 ? 8 : 12));
			MgSuppInspect insp = mDocService.findInspectBySuppWeek(persis.getSuppId(), week);
			if(insp == null) {
				MgSuppInspect temp = new MgSuppInspect();
				temp.setSupp(persis);
				temp.setWeek(week);
				temp.setBun(bun[i]);
				temp.setAlt(alt[i]);
				temp.setBlood(blood[i]);
				temp.setUrine(urine[i]);
				temp.setSp(sp[i]);
				temp.setCrp(crp[i]);
				temp.setEsr(esr[i]);
				temp.setEc(ec[i]);
				mDocService.save(temp);
			} else {
				insp.setBun(bun[i]);
				insp.setAlt(alt[i]);
				insp.setBlood(blood[i]);
				insp.setUrine(urine[i]);
				insp.setSp(sp[i]);
				insp.setCrp(crp[i]);
				insp.setEsr(esr[i]);
				insp.setEc(ec[i]);
				mDocService.update(insp);
			}
		}
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}

	@Override
	public void init(SysModule module, HttpServletRequest request) {
		if("docmgr_myastheniadocmgr_docEdit".equals(module.getModuleNo())) {
			String docId = request.getParameter("docId");
			request.setAttribute("docId", docId);
		}
		
		if("docmgr_myastheniadocmgr_common".equals(module.getModuleNo())) {
			String docId = request.getParameter("docId");
			if(docId != null && !"".equals(docId)) {
				request.setAttribute("patientDoc", mDocService.getPatientDoc(new Long(docId)));
			}
		} 
		
		if("docmgr_myastheniadocmgr_appr".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", mDocService.getPatientDoc(docId));
			request.setAttribute("mgAppr", mDocService.getMgApprByPatientId(docId));
		} 
		
		else if("docmgr_myastheniadocmgr_specific".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", mDocService.getPatientDoc(docId));
			request.setAttribute("spec", mDocService.getMgSpecificByPatientId(docId));
		} 
		
		else if("docmgr_myastheniadocmgr_treathis".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", mDocService.getPatientDoc(docId));
			BizMgTreathis treathis = mDocService.getMgTreathisByPatientId(docId);
			request.setAttribute("treathis", treathis);
			setMedicineFlag(treathis, request);
		} 
		
		else if("docmgr_myastheniadocmgr_mark".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", mDocService.getPatientDoc(docId));
			request.setAttribute("mark", mDocService.getMgMarkByPatientId(docId));
		} 
		
		else if("docmgr_myastheniadocmgr_clinical".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", mDocService.getPatientDoc(docId));
			request.setAttribute("clinical", mDocService.getMgClinicalByPatientId(docId));
		} 
		
		else if("docmgr_myastheniadocmgr_supplement".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", mDocService.getPatientDoc(docId));
			BizMgSupplement supplement = mDocService.getMgSupplementByPatientId(docId);
			request.setAttribute("supplement", supplement);
			
			request.setAttribute("immuTable", getImmuTable(supplement));
			request.setAttribute("inspTable", getInspectsTable(supplement));
			
			
		} 
		
		else if("docmgr_myastheniadocmgr_syndresult".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", mDocService.getPatientDoc(docId));
			request.setAttribute("syndresult", mDocService.getMgSyndresultByPatientId(docId));
		}
	}

	private void setMedicineFlag(BizMgTreathis treathis2,
			HttpServletRequest request) {
		if(treathis2 == null) {
			return;
		}
		List<MgTreathisWm> wmList = treathis2.getWm();
		for(int i=0; i<wmList.size(); i++) {
			MgTreathisWm wm = wmList.get(i);
			if("溴吡斯的明".equals(wm.getName())) {
				request.setAttribute("wm_1", 1);
			} else if("醋酸泼尼松".equals(wm.getName())) {
				request.setAttribute("wm_2", 1);
			} else if("甲泼尼龙".equals(wm.getName())) {
				request.setAttribute("wm_3", 1);
			} else if("硫唑嘌呤".equals(wm.getName())) {
				request.setAttribute("wm_4", 1);
			} else if("环孢素".equals(wm.getName())) {
				request.setAttribute("wm_5", 1);
			} else if("他克莫司".equals(wm.getName())) {
				request.setAttribute("wm_6", 1);
			} else if("雷公藤多苷".equals(wm.getName())) {
				request.setAttribute("wm_7", 1);
			} else if("环磷酰胺".equals(wm.getName())) {
				request.setAttribute("wm_8", 1);
			} else if("吗替麦考酚酯".equals(wm.getName())) {
				request.setAttribute("wm_9", 1);
			} else if("甲氨蝶呤".equals(wm.getName())) {
				request.setAttribute("wm_10", 1);
			} else if("丙种球蛋白".equals(wm.getName())) {
				request.setAttribute("wm_11", 1);
			} else if("血浆置换".equals(wm.getName())) {
				request.setAttribute("wm_12", 1);
			}
		}
	}

	private Object getInspectsTable(BizMgSupplement supp) {
		//其他项目
		List<MgSuppInspect> inspects = new ArrayList<MgSuppInspect>();
		if(supp != null) {
			inspects = supp.getInspects();
		}
		MgSuppInspect insp2 = null;
		MgSuppInspect insp4 = null;
		MgSuppInspect insp8 = null;
		MgSuppInspect insp12 = null;
		for(int i=0; i<inspects.size(); i++) {
			if(inspects.get(i).getWeek().intValue() == 2) {
				insp2 = inspects.get(i);
			} else if(inspects.get(i).getWeek().intValue() == 4) {
				insp4 = inspects.get(i);
			} else if(inspects.get(i).getWeek().intValue() == 8) {
				insp8 = inspects.get(i);
			} else if(inspects.get(i).getWeek().intValue() == 12) {
				insp12 = inspects.get(i);
			}
		}
		
		List inspTable = new ArrayList();
		String[] row1 = new String[4];
		row1[0] = insp2 == null ? "" : insp2.getBun();
		row1[1] = insp4 == null ? "" : insp4.getBun();
		row1[2] = insp8 == null ? "" : insp8.getBun();
		row1[3] = insp12 == null ? "" : insp12.getBun();
		inspTable.add(row1);
		String[] row2 = new String[4];
		row2[0] = insp2 == null ? "" : insp2.getAlt();
		row2[1] = insp4 == null ? "" : insp4.getAlt();
		row2[2] = insp8 == null ? "" : insp8.getAlt();
		row2[3] = insp12 == null ? "" : insp12.getAlt();
		inspTable.add(row2);
		String[] row3 = new String[4];
		row3[0] = insp2 == null ? "" : insp2.getBlood();
		row3[1] = insp4 == null ? "" : insp4.getBlood();
		row3[2] = insp8 == null ? "" : insp8.getBlood();
		row3[3] = insp12 == null ? "" : insp12.getBlood();
		inspTable.add(row3);
		String[] row4 = new String[4];
		row4[0] = insp2 == null ? "" : insp2.getUrine();
		row4[1] = insp4 == null ? "" : insp4.getUrine();
		row4[2] = insp8 == null ? "" : insp8.getUrine();
		row4[3] = insp12 == null ? "" : insp12.getUrine();
		inspTable.add(row4);
		String[] row5 = new String[4];
		row5[0] = insp2 == null ? "" : insp2.getSp();
		row5[1] = insp4 == null ? "" : insp4.getSp();
		row5[2] = insp8 == null ? "" : insp8.getSp();
		row5[3] = insp12 == null ? "" : insp12.getSp();
		inspTable.add(row5);
		String[] row6 = new String[4];
		row6[0] = insp2 == null ? "" : insp2.getCrp();
		row6[1] = insp4 == null ? "" : insp4.getCrp();
		row6[2] = insp8 == null ? "" : insp8.getCrp();
		row6[3] = insp12 == null ? "" : insp12.getCrp();
		inspTable.add(row6);
		String[] row7 = new String[4];
		row7[0] = insp2 == null ? "" : insp2.getEsr();
		row7[1] = insp4 == null ? "" : insp4.getEsr();
		row7[2] = insp8 == null ? "" : insp8.getEsr();
		row7[3] = insp12 == null ? "" : insp12.getEsr();
		inspTable.add(row7);
		String[] row8 = new String[4];
		row8[0] = insp2 == null ? "" : insp2.getEc();
		row8[1] = insp4 == null ? "" : insp4.getEc();
		row8[2] = insp8 == null ? "" : insp8.getEc();
		row8[3] = insp12 == null ? "" : insp12.getEc();
		inspTable.add(row8);
		
		return inspTable;
	}

	private List<MgSuppImmun> getImmuTable(BizMgSupplement supp) {
		//免疫学指标表格
		List<MgSuppImmun> immus = new ArrayList<MgSuppImmun>();
		if(supp != null) {
			immus = supp.getImmus();
		}
		MgSuppImmun immu2 = null;
		MgSuppImmun immu4 = null;
		MgSuppImmun immu8 = null;
		MgSuppImmun immu12 = null;
		for(int i=0; i<immus.size(); i++) {
			if(immus.get(i).getWeek().intValue() == 2) {
				immu2 = immus.get(i);
			} else if(immus.get(i).getWeek().intValue() == 4) {
				immu4 = immus.get(i);
			} else if(immus.get(i).getWeek().intValue() == 8) {
				immu8 = immus.get(i);
			} else if(immus.get(i).getWeek().intValue() == 12) {
				immu12 = immus.get(i);
			}
		}
		
		List immuTable = new ArrayList();
		String[] row1 = new String[4];
		row1[0] = immu2 == null ? "" : immu2.getIgg();
		row1[1] = immu4 == null ? "" : immu4.getIgg();
		row1[2] = immu8 == null ? "" : immu8.getIgg();
		row1[3] = immu12 == null ? "" : immu12.getIgg();
		immuTable.add(row1);
		String[] row2 = new String[4];
		row2[0] = immu2 == null ? "" : immu2.getC3();
		row2[1] = immu4 == null ? "" : immu4.getC3();
		row2[2] = immu8 == null ? "" : immu8.getC3();
		row2[3] = immu12 == null ? "" : immu12.getC3();
		immuTable.add(row2);
		String[] row3 = new String[4];
		row3[0] = immu2 == null ? "" : immu2.getCd4();
		row3[1] = immu4 == null ? "" : immu4.getCd4();
		row3[2] = immu8 == null ? "" : immu8.getCd4();
		row3[3] = immu12 == null ? "" : immu12.getCd4();
		immuTable.add(row3);
		String[] row4 = new String[4];
		row4[0] = immu2 == null ? "" : immu2.getCd8();
		row4[1] = immu4 == null ? "" : immu4.getCd8();
		row4[2] = immu8 == null ? "" : immu8.getCd8();
		row4[3] = immu12 == null ? "" : immu12.getCd8();
		immuTable.add(row4);
		String[] row5 = new String[4];
		row5[0] = immu2 == null ? "" : immu2.getSil2r();
		row5[1] = immu4 == null ? "" : immu4.getSil2r();
		row5[2] = immu8 == null ? "" : immu8.getSil2r();
		row5[3] = immu12 == null ? "" : immu12.getSil2r();
		immuTable.add(row5);
		String[] row6 = new String[4];
		row6[0] = immu2 == null ? "" : immu2.getIfn();
		row6[1] = immu4 == null ? "" : immu4.getIfn();
		row6[2] = immu8 == null ? "" : immu8.getIfn();
		row6[3] = immu12 == null ? "" : immu12.getIfn();
		immuTable.add(row6);
		String[] row7 = new String[4];
		row7[0] = immu2 == null ? "" : immu2.getAntissa();
		row7[1] = immu4 == null ? "" : immu4.getAntissa();
		row7[2] = immu8 == null ? "" : immu8.getAntissa();
		row7[3] = immu12 == null ? "" : immu12.getAntissa();
		immuTable.add(row7);
		String[] row8 = new String[4];
		row8[0] = immu2 == null ? "" : immu2.getAntissb();
		row8[1] = immu4 == null ? "" : immu4.getAntissb();
		row8[2] = immu8 == null ? "" : immu8.getAntissb();
		row8[3] = immu12 == null ? "" : immu12.getAntissb();
		immuTable.add(row8);
		String[] row9 = new String[4];
		row9[0] = immu2 == null ? "" : immu2.getAna();
		row9[1] = immu4 == null ? "" : immu4.getAna();
		row9[2] = immu8 == null ? "" : immu8.getAna();
		row9[3] = immu12 == null ? "" : immu12.getAna();
		immuTable.add(row9);
		return immuTable;
	}

	public PatientDoc getPatientDoc() {
		return patientDoc;
	}

	public void setPatientDoc(PatientDoc patientDoc) {
		this.patientDoc = patientDoc;
	}

	public BizMgAppr getMgAppr() {
		return mgAppr;
	}

	public void setMgAppr(BizMgAppr mgAppr) {
		this.mgAppr = mgAppr;
	}

	public BizMgSpecific getSpec() {
		return spec;
	}

	public void setSpec(BizMgSpecific spec) {
		this.spec = spec;
	}

	public BizMgTreathis getTreathis() {
		return treathis;
	}

	public void setTreathis(BizMgTreathis treathis) {
		this.treathis = treathis;
	}

	public BizMgMark getMark() {
		return mark;
	}

	public void setMark(BizMgMark mark) {
		this.mark = mark;
	}

	public BizMgClinical getClinical() {
		return clinical;
	}

	public void setClinical(BizMgClinical clinical) {
		this.clinical = clinical;
	}

	public BizMgSyndresult getSyndresult() {
		return syndresult;
	}

	public void setSyndresult(BizMgSyndresult syndresult) {
		this.syndresult = syndresult;
	}

	public BizMgSupplement getSupplement() {
		return supplement;
	}

	public void setSupplement(BizMgSupplement supplement) {
		this.supplement = supplement;
	}
}
